package org.afolsn.user

import org.springframework.orm.jpa.JpaCallback

class UserService {

	def jpaTemplate
	def springSecurityService
	
    static transactional = true

    User loadUser(String login) {
		jpaTemplate.execute( { entityManager ->
			def query = entityManager.createQuery("select from org.afolsn.user.User where login = '" + login + "'")
			def result = query.resultList
			if(result.size() == 1)
				return result[0]
			else
				return null
		} as JpaCallback )
    }
	
	User registerUser(User user) {
		User.withTransaction {
			user.password = springSecurityService.encodePassword(user.password)
			user.save(flush:true)
		}
	}
}
